;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This file is part of ICCLE2.
;
; ICCLE2 is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; ICCLE2 is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with ICCLE2.  If not, see <http://www.gnu.org/licenses/>.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun counts (tbl &optional (str t))
  (let  ((klassh (klassh tbl)))
    (format str "~%;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;~%")
    (format str "~a rows" (table-rows tbl))
    (dotimes (klass (discrete-cardinality klassh))
      (format str "~%    F(~a) = ~a" 
              (sym->thing klassh klass) 
              (f1 tbl klass))
      (doarray 
          (colh col (table-headers tbl))
        (unless (eq colh klassh)
          (terpri)
          (dotimes (val (discrete-cardinality colh))
            (format str "        with F(~a=~a|~a) = ~a~%"
                    (discrete-name colh)
                    (sym->thing colh val)
                    (sym->thing klassh klass) 
                    (f3 tbl klass col val))))))))

(defun klassh (tbl)
  (aref (table-headers tbl) (1- (table-cols tbl))))

(defun f0 (tbl)
  (table-rows tbl))


(defun f1 (tbl klass)
  (aref 
   (xindex-counts 
    (discrete-xindex
     (klassh tbl))) 
   klass klass))

(defun p (tbl klass)
  (/ (f1 tbl klass) (f0 tbl)))

(defun peh (tbl klass col val)
  (/ (f3 tbl klass col val) (f1 tbl klass)))

(defun f3 (tbl &optional klass col val)
   (aref
    (xindex-counts 
     (discrete-xindex 
      (aref (table-headers tbl) col)))
    klass val))